/*
 *
 * Copyright (c) 2008, All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Portions of the code may be copied from Google Collections
 * or Apache Commons projects.
 */

package com.coury.dasle.helper;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import com.google.common.collect.Lists;

public final class FileHelper {
	private FileHelper() {
		throw new AssertionError("Cannot instantiate");
	}

	/**
     * Reads a reader as a list of strings. each item represents one line in the reader.
     *
     * @param file the file to read
     * @return List of strings.
     * @throws NullPointerException: if reader is null.
     * @throws java.io.IOException   if an IO error occurs.
     */
    public static List<String> readAsStringList(File file) throws IOException {
    	BufferedReader reader = 
    		new BufferedReader(new InputStreamReader(new FileInputStream(file)));
    	return readAsStringList(reader);
    }

    /**
     * Reads a reader as a list of strings. each item represents one line in the reader.
     *
     * @param reader a reader
     * @return List of strings.
     * @throws NullPointerException: if reader is null.
     * @throws java.io.IOException   if an IO error occurs.
     */
    public static List<String> readAsStringList(BufferedReader reader)
            throws IOException {
        checkNotNull(reader, "reader cannot be null");
        try {
            String s;
            List<String> res = Lists.newArrayList();
            while ((s = reader.readLine()) != null)
                res.add(s);
            return res;
        }
        finally {
            closeSilently(reader);
        }
    }
    
    private static void checkNotNull(Object cannotBeNull, String error) {
    	if (cannotBeNull == null) {
    		throw new NullPointerException(error);
    	}
	}

	/**
     * closes the <code>closeables</code> silently, meaning that if the Closeable is null,
     * or if it throws an exception during close() operation it only creates a system error output,
     * does not throw an exception.
     * this is especially useful when you need to close one or more resources in finally blocks.
     * This method should only be called in finalize{} blocks or wherever it really makes sense.
     *
     * @param closeables zero or more closeable.
     */
    public static void closeSilently(Closeable... closeables) {
        // if closeables is null, return silently.
        if (closeables == null) return;

        for (Closeable closeable : closeables) {
            try {
                if (closeable != null)
                    closeable.close();
            }
            catch (IOException e) {
                System.err.println("IO Exception during closing stream (" + closeable + ")." + e);
            }
        }
    }    
}
